package com.hotelvip.controller;

import com.hotelvip.common.utils.JSONResult;
import io.swagger.annotations.Api;
/*import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;*/
import org.springframework.web.bind.annotation.*;

/**
 * @author Lzh
 * @date 2020/12/21 10:03
 */
@RestController
@RequestMapping("rest/solr")
@Api(tags = {"SolrController solr搜索引擎"}, description = "Solr 全文检索")
public class SolrController {
   /* @Autowired
    private SolrClient solrClient;

    @ApiOperation(value = "添加文档", notes = "向文档中添加域，必须有id域，域的名称必须在scheme.xml中定义", httpMethod = "GET")
    @GetMapping("/insert")
    public JSONResult validator(@ApiParam("客户用户名称") @RequestParam String name,
                                @ApiParam("地址") @RequestParam String addr) {
        try {
            String idStr = String.valueOf(System.currentTimeMillis());

            SolrInputDocument document = new SolrInputDocument();
            document.setField("id", idStr);
            document.setField("name", name);
            document.setField("addr", addr);

            //把文档写入搜索库
            solrClient.add("jymj-ht",document);
            solrClient.commit("jymj-ht");//如果配置文件中没有指定core，这个方法的第一个参数就需要指定core名称client.commit("core");

            return JSONResult.ok(idStr);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return JSONResult.errorMsg("添加失败");


    }

    @ApiOperation(value = "更新文档内容",notes = "更新文档内容，跟添加的区别是：id不能变，其他的可以变",httpMethod = "GET")
    @GetMapping(value = "/updateDocument")
    public JSONResult updateDocument(@ApiParam(value = "idStr") @RequestParam String idStr,
                                 @ApiParam(value = "name") @RequestParam String name,
                                 @ApiParam(value = "addr") @RequestParam String age) throws Exception{
        // 创建一个文档对象, 向文档中添加域，必须有id域，域的名称必须在scheme.xml中定义
        SolrInputDocument document = new SolrInputDocument();
        document.setField("id", idStr);
        document.setField("name", name);
        document.setField("age",age);
        // 把文档对象写入索引库
        solrClient.add("jymj-ht",document);
        // 提交
        solrClient.commit("jymj-ht");
        return JSONResult.ok(document);
    }

    @ApiOperation(value = "查询文档内容",notes = "查询文档内容",httpMethod = "GET")
    @GetMapping(value = "/select")
    public JSONResult select(@ApiParam(value = "条件",defaultValue = "*:*") @RequestParam String condition,
                                @ApiParam(value = "连接文件夹 默 jymj-ht",defaultValue = "jymj-ht") @RequestParam String collection,
                                @ApiParam(value = "分页起始 默 1",defaultValue = "1") @RequestParam Integer pageStart,
                                @ApiParam(value = "分页结束 默 10",defaultValue = "10") @RequestParam Integer pageEnd) throws Exception {
        // 创建一个查询条件
        SolrQuery solrQuery = new SolrQuery();
        // 设置查询条件
        solrQuery.setQuery(condition);
        // 设置分页
        solrQuery.setStart(pageStart);
        solrQuery.setRows(pageEnd);
        // 执行查询
        QueryResponse query = solrClient.query(collection,solrQuery);
        // 取查询结果
        SolrDocumentList solrDocumentList = query.getResults();

        System.out.println("总记录数：" + solrDocumentList.getNumFound());

        for (SolrDocument sd : solrDocumentList) {
            System.out.println(sd.get("id"));
            System.out.println(sd.get("name"));
            System.out.println(sd.get("addr"));
        }
        return JSONResult.ok(solrDocumentList);
    }

    @ApiOperation(value = "删除文档",notes = "删除文档",httpMethod = "DELETE")
    @DeleteMapping(value = "deteleDocument")
    public JSONResult deteleDocument(@ApiParam(value = "连接文件夹 默 jymj-ht" ,defaultValue = "jymj-ht") @RequestParam String collection,
                                 @ApiParam(value = "idStr") @RequestParam String idStr) throws Exception {
        // 根据id删除
        UpdateResponse response = solrClient.deleteById(collection, idStr);
        // 根据条件删除
        // httpSolrServer.deleteByQuery("");
        // 提交
        solrClient.commit(collection);

        return JSONResult.ok(response);
    }*/
}
